var express = require('express');
var router = express.Router();

// 导入db，操作数据库
const { getAll, getById, exec } = require('../db');

/**
 * 查找报告人id
 * GET /report
 */
router.get('/', async (req, res) => {
	const { username } = req.query;
	let sql = `SELECT user_id FROM gg_user WHERE user_name='${username}'`;
	const data = await getById(sql);
	res.send({
		code: 0,
		message: '获取成功',
		result: data,
	});
});

/**
 * 新增
 * POST /report
 */

router.post('/', async (req, res) => {
	const { user_id, event_type, event_addr, event_mark, event_describe } =
		req.body;
	console.log(user_id, event_type, event_addr, event_mark, event_describe);
	let sql = `INSERT INTO gg_event (user_id, event_type, event_addr, event_mark, event_describe) VALUES (${user_id},'${event_type}','${event_addr}','${event_mark}','${event_describe}')`;
	const data = await exec(sql);
	console.log(data);
	// 编写响应内容
	res.send({
		code: 0,
		message: '新增成功',
		result: {
			event_id: data.insertId,
			user_id,
			event_type,
			event_addr,
			event_mark,
			event_describe,
		},
	});
});

module.exports = router;
